<template>
  <div class="openDataScreenDialog">
    <el-dialog
      title="数据展示筛选"
      v-model="visible"
      width="640px"
      :show-close="true"
      @close="Closed()">
      <div class="content">
        <el-checkbox
          v-model="checkAll"
          :indeterminate="isIndeterminate"
          @change="handleCheckAllChange">
          全选
        </el-checkbox>
        <el-checkbox-group
          v-model="checkedData"
          @change="handleCheckedDataChange">
          <el-checkbox v-for="(item, index) in dataOptions" :key="index" :label="item">
            {{ item.name }}
          </el-checkbox>
        </el-checkbox-group>
      </div>
      <template #footer>
        <span class="footer">
          <el-button size="mini" @click="Closed">取消</el-button>
          <el-button size="mini" type="primary" @click="handleClickConfirm()">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import store from "@/store";
import { ElMessage } from "element-plus";
export default {
  name: 'OpenDataScreenDialog',
  props: {
    visible: {
      type: Boolean,
      default: () => {
        return false;
      }
    },
    dataOptions: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      checkAll: false,
      isIndeterminate: false,
      checkedData: [],
    }
  },
  watch: {
    visible: {
      handler(val) {
        if(val) {
          this.$nextTick(() => {
            this.checkAll = false;
            this.isIndeterminate = false;
            this.checkedData = [];
          })
        }
      }
    }
  },
  mounted() {
    
  },
  methods: {
    // 修饰关闭dialog
    Closed() {
      store.commit('OPENDATASCREEN', null);
      this.$emit('update:visible', false);
    },
    // 全选
    handleCheckAllChange(val) {
      this.checkedData = val ? this.dataOptions : [];
      this.isIndeterminate = false;
    },
    // 单个选择
    handleCheckedDataChange(val) {
      let checkedCount = val.length;
      this.checkAll = checkedCount === this.dataOptions.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.dataOptions.length;
    },
    // 确定筛选
    handleClickConfirm() {
      console.log(this.checkedData)
      if(this.checkedData.length === 0) {
        ElMessage({
          type: "warning",
          message: "请选择展示数据！",
        });
        return false;
      }
      this.$emit('handleScreen', this.checkedData);
      this.Closed();
    }
  }
}
</script>

<style lang="scss" scoped>
.openDataScreenDialog {

}
</style>